
import java.util.*;
import java.io.*;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPException;
//original code from here http://abhijeetmaharana.com/blog/2007/10/28/writing-a-gtalk-jabberxmpp-client/#comment-190961
//Abhijeet Maharana abhijeet.maharana@gmail.com
//Code is free to use for whatever purpose cnoffsin@gmail.com

public class JavaXMPPBot implements MessageListener
{
	XMPPTCPConnection connection;
	
	public void login(String userName, String password) throws XMPPException, SmackException, IOException
	{
		//here 192.168.40.21 is the actual ip of the server and jabber is the jabber domain (the second half of the persons userid name@jabber)
		ConnectionConfiguration config = new ConnectionConfiguration("192.168.40.21", 5223, "jabber");
		
		//next 2 lines use the DummySSLSocketFactory class to make it connect with servers that have self-signed certs
		//Of course realizing that this is possibly exploited with man in the middle attack so don't put your freakin social security number in here!
		config.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);
		config.setSocketFactory(new DummySSLSocketFactory());
		
		connection = new XMPPTCPConnection(config);

		connection.connect();
		connection.login(userName, password);
		
	}
	
	public void sendMessage(String message, String to) throws XMPPException, NotConnectedException
	{
		//Chat chat = connection.getChatManager().createChat(to, this);
		Chat chat = ChatManager.getInstanceFor(connection).createChat(to, this);
		chat.sendMessage(message);
	}
	
	//dumps to the console everyone on that users buddy list, not necessarily who is connected to the jabber server in total
	public void displayBuddyList()
	{
		Roster roster = connection.getRoster();
		Collection<RosterEntry> entries = roster.getEntries();
		
		System.out.println("\n\n" + entries.size() + " buddy(ies):");
		for(RosterEntry r:entries)
		{
			System.out.println(r.getUser());
		}
	}

	public void disconnect() throws NotConnectedException
	{
		connection.disconnect();
	}
	
	public void processMessage(Chat chat, Message message)
	{
		//repeats what sender says to console
		if(message.getType() == Message.Type.chat)
	        System.out.println(chat.getParticipant() + " says: " + message.getBody());
		
		//get the persons name that sent it
		String receiver=chat.getParticipant();
		
		//take what sender says and respond it
		//this is where you can put your logic in to respond to a message
		//in this case you message what, the bot responds this is just a test
		if(message.getBody().equalsIgnoreCase("what"))
		{
			try {
				sendMessage("This is just a test", receiver);
			} catch (NotConnectedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (XMPPException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
		}
					
    }
	
	public static void main(String args[]) throws XMPPException, IOException, SmackException
	{
		
		
		
		// declare variables
		JavaXMPPBot c = new JavaXMPPBot();
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String msg;


		// turn on the enhanced debugger
		//XMPPConnection.DEBUG_ENABLED = true;


		// provide your login information here
		c.login("sender", "test123");

		
		c.displayBuddyList();
		System.out.println("-----");
		System.out.println("Enter your message in the console.");
		System.out.println("All messages will be sent to receiver");
		System.out.println("-----\n");
		c.sendMessage("test", "receiver@jabber");
		
		while( !(msg=br.readLine()).equals("bye"))
		{
			// your receiver jabber address
			c.sendMessage(msg, "receiver@jabber");			
		}

		c.disconnect();
		System.exit(0);
	}
	
}